package com.zhanghe.study.study_scala.oop

/**
 * 继承
 *
 * @author zh
 * @date 2021/5/2 13:47
 */
object TestExtends {
  def main(args: Array[String]): Unit = {
    val tiger = new Tiger
    tiger.name = "东北虎"
    tiger.sayHello
    tiger.run
    tiger.who

    println("***********")
    val parent:Parent = new Child
    val child:Child = new Child
    // 5
    println(parent.age)
    // 5
    println(child.age)

    // 动态混入
    val seal = new Seal with Swimmable {
      override def swig(): Unit = {
        println("我会游泳哦")
      }
    }
    seal.swig()

    var seal2 = new Seal with Swimmable with Breathable{
      override def swig(): Unit = {
        println("我会游泳啊")

      }

      override def breath(): Unit = {

        println("我会呼吸啊")
      }
    }

  }
}

// 没有父类，直接继承特征
// class 类名 extends 特征
// 如果同时继承多个特征的话需要使用with     class 类名 extends 特征1 with 特征2 with 特征3
class Animal extends Breathable {
  var name: String = "动物"

  def sayHello: Unit = {
    println("你好，我是" + name)
  }

  def who:Unit = {
    println("我是动物")
  }

  override def breath(): Unit = {
    println("我会呼吸")
  }
}

// 有父类，需要先继承父类，再继承特征
//  class 类名 extends 父类 with 特征1 with 特征2 with 特征3
class Bird extends Animal with Flyable{
  override def fly(): Unit = {
    println("我会飞")
  }
}

class Dog extends Animal with Swimmable{
  override def swig(): Unit = {

    println("我会狗刨")
  }
}

class Tiger extends Animal {

  def run: Unit = {
    println("I can run")
  }

  // 方法重写
  override def who: Unit = {
    println("我是老虎，森林之王")
  }
}

class Seal{

}

// 定义特征
trait Breathable{
  def breath()
}

trait Flyable{
  def fly()
}

trait Swimmable{
  def swig()
}

abstract class Parent{
  // 抽象字段
  var age:Int

  var name:String = _

  // 抽象方法
  def breath()
}

class Child extends Parent{
  override var age:Int = 5

  override def breath(): Unit = {
    println("用鼻子呼吸")
  }
}
